<?php
/**
 * Created by PhpStorm.
 * User: kongqi
 * Date: 2019/11/30
 * Time: 2:59
 */

namespace App\Plugin\HostServer\Front;

use App\Plugin\HostServer\Models\PnHostSvCode;
use App\Plugin\HostServer\Models\PnHostSvDomainOrder;
use App\Plugin\HostServer\Models\PnHostSvGood;
use App\Plugin\HostServer\Models\PnHostSvOrder;

use App\Plugin\HostServer\Models\PnHostSvUser;
use App\Plugin\HostServer\Models\PnHostSvUserLog;
use App\Plugin\HostServer\Models\PnHostSvUserMoneyLog;
use App\Plugin\HostServer\Services\NotifyService;

use App\TraitClass\CheckFormTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UserController extends BaseController
{
    use CheckFormTrait;

    public function index()
    {
        $this->setSeo('会员中心', '会员中心', '会员中心');
        $user_log = PnHostSvUserLog::where('group_type', 'login')->idDesc()->limit(5)->get();
        $data = [
            'log' => $user_log
        ];
        return $this->display($data);
    }

    //更新操作
    public function handle(Request $request)
    {
        $type = $request->input('type');
        if (empty($type)) {
            return $this->returnErrorApi('缺少操作类型');
        }
        if (!in_array($type, ['buy_password', 'thumb', 'password','email'])) {
            return $this->returnErrorApi('缺少操作类型');
        }
        $method = 'update' . convert_under_line($type);

        return $this->$method($request);

    }

    public function updateEmail($request)
    {
        $data = $request->all();
        $check_data = [
            'email' => 'email',
            'old_email' => 'email',
            'old_code' => function ($attribute, $value, $fail) {
                if ($value != PnHostSvCode::checkCode($value, 'email', \request()->input('old_email'))) {
                    $fail('旧邮箱验证码无效');
                }

            },
            'code' => function ($attribute, $value, $fail) {
                if ($value != PnHostSvCode::checkCode($value, 'email', \request()->input('email'))) {
                    $fail('新邮箱验证码无效');
                }

            }

        ];

        $error = $this->checkForm($data, $check_data, [], ['email' => '邮箱账户']);
        if (count($error) > 0) {
            $errors = $this->checkFormErrorFormat($error);
            return $this->returnErrorApi($errors['msg']);
        }
        //检查旧密码是否正确
        $user = user('', 'host_server');
        //验证邮箱验证码
        $email=$request->input('email');
        $old_email=$request->input('old_email');
        $user->email = $request->input('email');
        $r = $user->save();
        if ($r) {
            //更新验证码1
            PnHostSvCode::codeUpdate('email',$email);
            //更新验证码2
            PnHostSvCode::codeUpdate('email',$old_email);
            return $this->returnOkApi('更新成功',['url'=>plugin_route('hostserver.user')]);
        }
        return $this->returnOkApi('无更新');
    }

    public function updatePassword($request)
    {
        $data = $request->all();
        $check_data = [
            'password' => ['required', 'string', 'min:6', 'confirmed'],
            'old_password' => 'required'
        ];

        $error = $this->checkForm($data, $check_data, [], ['email' => '邮箱账户']);
        if (count($error) > 0) {
            $errors = $this->checkFormErrorFormat($error);
            return $this->returnErrorApi($errors['msg']);
        }
        //检查旧密码是否正确
        $user = user('', 'host_server');

        if (!Hash::check($request->input('old_password'), $user['password'])) {
            return $this->returnOkApi('旧密码不对');
        }
        $user->password = $request->password;
        $r = $user->save();
        if ($r) {
            return $this->returnOkApi('更新成功',['url'=>plugin_route('hostserver.user')]);
        }
        return $this->returnOkApi('无更新');
    }

    public function updateBuyPassword($request)
    {
        $data = $request->all();
        $check_data = [
            'buy_password' => ['required', 'string', 'min:6', 'confirmed'],
            'password' => 'required'
        ];

        $error = $this->checkForm($data, $check_data, [], ['email' => '邮箱账户']);
        if (count($error) > 0) {
            $errors = $this->checkFormErrorFormat($error);
            return $this->returnErrorApi($errors['msg']);
        }
        //检查旧密码是否正确
        $user = user('', 'host_server');

        if (!Hash::check($request->input('password'), $user['password'])) {
            return $this->returnOkApi('旧密码不对');
        }
        $user->buy_password = $request->buy_password;
        $r = $user->save();
        if ($r) {
            return $this->returnOkApi('更新成功',['url'=>plugin_route('hostserver.user')]);
        }
        return $this->returnOkApi('无更新');
    }

    public function seting($type = 'base', Request $request)
    {

        $title = '修改';
        $this->setSeo($title, $title, $title);

        $data = [
            'type' => $type
        ];
        $this->setModelControllerView('seting_' . $type);
        return $this->display($data);
    }



    //对账单
    public function bill($type = 'server', Request $request)
    {

        $pagesize = 10;
        $user = user('', 'host_server');
        $nav = [
            [
                'url' => nroute('plugin.hostserver.user.bill.type', ['type' => 'server']),
                'name' => '购买记录',
                'type' => 'server'
            ],
            [
                'url' => nroute('plugin.hostserver.user.bill.type', ['type' => 'money_log']),
                'name' => '资金变动记录',
                'type' => 'money_log'
            ]

        ];
        $title = '记录';
        switch ($type) {

            case 'money_log':
                $list = PnHostSvUserMoneyLog::where('user_id', $user['id'])->orderBy('id', 'desc')->paginate($pagesize);
                $title = '资金变动记录';
                break;

        }
        $this->setSeo($title, $title, $title);
        $data = [
            'type' => $type,
            'list' => $list,
            'user' => $user,
            'pager' => $list ? $list->links() : ''
        ];
        return $this->display($data);
    }

    //服务器订单记录
    public function order()
    {
        $pagesize = 10;
        $user = user('', 'host_server');
        $list = PnHostSvOrder::where('user_id', $user['id'])->orderBy('id', 'desc')->paginate($pagesize);
        $title = '服务器购买记录';
        $data = [
            'title' => $title,
            'type' => 'server',
            'list' => $list,
            'user' => $user,
            'pager' => $list ? $list->links() : ''
        ];
        $this->setModelControllerView('bill');
        return $this->display($data);
    }
    //服务器订单记录
    public function orderDomain ()
    {
        $pagesize = 10;
        $user = user('', 'host_server');
        $list = PnHostSvDomainOrder::with('info')->where('user_id', $user['id'])->orderBy('id', 'desc')->paginate($pagesize);
        $title = '域名注册记录';
        $data = [
            'title' => $title,
            'type' => 'domain',
            'list' => $list,
            'user' => $user,
            'pager' => $list ? $list->links() : ''
        ];
        $this->setModelControllerView('bill');
        return $this->display($data);
    }






}